﻿using LanqiQAwebapi.Interface;
using LanqiQAwebapi.Models;
using Microsoft.EntityFrameworkCore;

namespace LanqiQAwebapi.Todo
{
    public class essay : Iessay
          
    {
        private readonly LanQiQaDBContext _LanQiQaDBContext;

        public essay(LanQiQaDBContext lanQiQaDBContext) 
        {

            _LanQiQaDBContext= lanQiQaDBContext;
        }
        /// <summary>
        /// 默认推荐
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public List<Qa> GetEssay(int page = 1, int pagesize = 10)
        {
         var query=   _LanQiQaDBContext.Qas.AsQueryable();


            return query.Skip((page - 1) * pagesize).Take(page).ToList();
           
        }
        /// <summary>
        /// 用户关注的回答没有关注默认推荐
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <param name="CurttrUserid"></param>
        /// <returns></returns>
        public List<Qa> GetEssayByFollow(int page, int pagesize, int CurttrUserid)
        {
                var query= _LanQiQaDBContext.Follows.Where(f => f.UserId == CurttrUserid).SelectMany(f => f.FollowedUser.Qas);
            if (query.Count() > 0)
            {
                return query.Skip((page - 1) * pagesize).Take(page).ToList();//有关注
            }
            else 
            {
                 var defaultQuery = _LanQiQaDBContext.Qas.AsQueryable();
                return defaultQuery.Skip((page-1)*pagesize).Take(pagesize).ToList();//无关住
            } 

        }
        /// <summary>
        /// 根据用户的指定问题来获取文章
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <param name="questions"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Qa> GetEssayByQuestion(int page, int pagesize, string questions)
        {
            var query = _LanQiQaDBContext.Qas.Where(o => o.Question== questions).AsQueryable();


            return query.Skip((page - 1) * pagesize).Take(page).ToList();
          
        }

        public List<Qa> GetHotQuestion()
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 用户搜索回答
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <param name="questions"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Qa> UserSearch(int page, int pagesize, string questions)
        {

            var query = _LanQiQaDBContext.Qas.Where(o => o.Question.Contains( questions)/*||o.TextFilePath.Contains(questions)*/).AsQueryable();

            if (query.Count() > 0)
            {
                return query.Skip((page - 1) * pagesize).Take(page).ToList();
            }
            else 
            {
                var defaultquery = _LanQiQaDBContext.Qas.AsQueryable();


                return query.Skip((page - 1) * pagesize).Take(page).ToList();

            }
        }
    }
}
